import { watch } from 'chokidar';
import { Stats } from 'fs';

interface IChange {
    type: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir';
    payload: {
        path: string;
        stats?: Stats;
    }
}
export default (paths: string | ReadonlyArray<string>, cb: (event: IChange) => void) => {
    const watcher = watch(paths, {
        ignored: /(^|[\/\\])\../, // ignore dotfiles
        persistent: true,
        ignoreInitial: true,
    });
    watcher.on('all', (eventName: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir', path: string, stats?: Stats) => {
        cb({
            type: eventName, payload: {
                path, stats
            }
        })
    })
}